//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.view;

import org.prx.prp.BackgroundDataService;
import org.prx.prp.IBackgroundDataService;
import org.prx.prp.R;
import org.prx.prp.MediaPlaybackLayout;

import android.util.Log;
import android.view.View;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.app.TabActivity;
import android.widget.TextView;

public class LiveViewActivity extends TabActivity {
	private TabHost mTabHost;
	
	private IBackgroundDataService mDataService = null;
    
    private ServiceConnection mConnection = new ServiceConnection() {
   	 public void onServiceConnected(ComponentName className, IBinder service){
   		 mDataService = IBackgroundDataService.Stub.asInterface(service);
   		 try {
   			 if( mDataService.isSyncing()){
   				 System.out.println("Hey, the dataservice is still syncing...");
   			 } else {
   				 System.out.println("Actually, I guess it's not syncing. Let's start that up...");
   				 mDataService.startSync();
   			 }
   		 } catch(RemoteException e){
   			 e.printStackTrace();
   		 }
   	 }
   	 
   	 public void onServiceDisconnected(ComponentName className){
   		 Log.d("PRPAND", "SOMETHING WENT VERY WRONG");
   	 }
    };
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.liveviewlayout);

		bindService(new Intent(this, BackgroundDataService.class), mConnection, BIND_AUTO_CREATE);
		
		mTabHost = getTabHost();
		
		Intent intent = new Intent(this,FeaturedLiveListActivity.class);
		TabHost.TabSpec tabspec;
		tabspec = mTabHost.newTabSpec("featured_tab").setIndicator(
				new MyView(this,"Featured",4)).setContent(intent);
		mTabHost.addTab(tabspec);
		
		intent = new Intent().setClass(this, ProgramListActivity.class);
		tabspec = mTabHost.newTabSpec("program_tab").setIndicator(new MyView(this,"Program",4)).setContent(intent);
		mTabHost.addTab(tabspec);

		intent = new Intent().setClass(this, StateListActivity.class);
		tabspec = mTabHost.newTabSpec("state_tab").setIndicator(new MyView(this,"State",4)).setContent(intent);
		mTabHost.addTab(tabspec);
		
		intent = new Intent().setClass(this, LocalListActivity.class);
		tabspec = mTabHost.newTabSpec("local_tab").setIndicator(new MyView(this,"Local",4)).setContent(intent);
		mTabHost.addTab(tabspec);
		
		/* We start the local activity so that location is calculated at the beginning */
		mTabHost.setCurrentTab(3);
		
		/* And then default to the featured list */
		mTabHost.setCurrentTab(0);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	
    	menu.add(0,0,0,"On Demand").setIcon(R.drawable.tab_ondemand);
    	menu.add(1,1,1,"Favorites").setIcon(R.drawable.tab_favorites);
    	menu.add(2,2,2,"Search").setIcon(R.drawable.tab_search);

    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent mIntent;
    	switch (item.getItemId()) {
    	case 0:
    		mIntent = new Intent(this, OnDemandViewActivity.class);
    		startActivity(mIntent);
    		break;
    	case 1:
    		mIntent = new Intent(this, FavoritesLiveActivity.class);
    		startActivity(mIntent);
    		break;
    	case 2:
    		mIntent = new Intent(this, SearchViewActivity.class);
    		startActivity(mIntent);
    		break;
    	}
    	return super.onOptionsItemSelected(item);
    }

	@Override
	public void onDestroy() {
		unbindService(mConnection);
		super.onDestroy();
		//((MediaPlaybackLayout)findViewById(R.id.media_player)).unRegisterListeners();
	}
}

